﻿using System;
using System.Globalization;
using System.IO;
using System.Text;
using MessagePack.Formatters;
using MessagePack.Internal;
using MessagePack.Resolvers;

namespace MessagePack
{
	// Token: 0x020000FD RID: 253
	public static class MessagePackSerializer
	{
		// Token: 0x0600038D RID: 909 RVA: 0x0000CE6E File Offset: 0x0000B26E
		public static string ToJson<T>(T obj)
		{
			return MessagePackSerializer.ToJson(MessagePackSerializer.Serialize<T>(obj));
		}

		// Token: 0x0600038E RID: 910 RVA: 0x0000CE7B File Offset: 0x0000B27B
		public static string ToJson<T>(T obj, IFormatterResolver resolver)
		{
			return MessagePackSerializer.ToJson(MessagePackSerializer.Serialize<T>(obj, resolver));
		}

		// Token: 0x0600038F RID: 911 RVA: 0x0000CE8C File Offset: 0x0000B28C
		public static string ToJson(byte[] bytes)
		{
			if (bytes == null || bytes.Length == 0)
			{
				return string.Empty;
			}
			StringBuilder stringBuilder = new StringBuilder();
			MessagePackSerializer.ToJsonCore(bytes, 0, stringBuilder);
			return stringBuilder.ToString();
		}

		// Token: 0x06000390 RID: 912 RVA: 0x0000CEC4 File Offset: 0x0000B2C4
		private static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
		{
			int num = 0;
			switch (MessagePackBinary.GetMessagePackType(bytes, offset))
			{
			case MessagePackType.Integer:
			{
				byte b = bytes[offset];
				if (224 <= b && b <= 255)
				{
					builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out num));
				}
				else if (0 <= b && b <= 127)
				{
					builder.Append(MessagePackBinary.ReadByte(bytes, offset, out num));
				}
				else if (b == 208)
				{
					builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out num));
				}
				else if (b == 209)
				{
					builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out num));
				}
				else if (b == 210)
				{
					builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out num));
				}
				else if (b == 211)
				{
					builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out num));
				}
				else if (b == 204)
				{
					builder.Append(MessagePackBinary.ReadByte(bytes, offset, out num));
				}
				else if (b == 205)
				{
					builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out num));
				}
				else if (b == 206)
				{
					builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out num));
				}
				else if (b == 207)
				{
					builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out num));
				}
				return num;
			}
			case MessagePackType.Boolean:
				builder.Append((!MessagePackBinary.ReadBoolean(bytes, offset, out num)) ? "false" : "true");
				return num;
			case MessagePackType.Float:
				builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out num));
				return num;
			case MessagePackType.String:
				MessagePackSerializer.WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out num), builder);
				return num;
			case MessagePackType.Binary:
				builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out num)) + "\"");
				return num;
			case MessagePackType.Array:
			{
				uint num2 = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out num);
				int num3 = num;
				offset += num;
				builder.Append("[");
				int num4 = 0;
				while ((long)num4 < (long)((ulong)num2))
				{
					num = MessagePackSerializer.ToJsonCore(bytes, offset, builder);
					offset += num;
					num3 += num;
					if ((long)num4 != (long)((ulong)(num2 - 1U)))
					{
						builder.Append(",");
					}
					num4++;
				}
				builder.Append("]");
				return num3;
			}
			case MessagePackType.Map:
			{
				uint num5 = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out num);
				int num6 = num;
				offset += num;
				builder.Append("{");
				int num7 = 0;
				while ((long)num7 < (long)((ulong)num5))
				{
					MessagePackType messagePackType = MessagePackBinary.GetMessagePackType(bytes, offset);
					if (messagePackType == MessagePackType.String || messagePackType == MessagePackType.Binary)
					{
						num = MessagePackSerializer.ToJsonCore(bytes, offset, builder);
					}
					else
					{
						builder.Append("\"");
						num = MessagePackSerializer.ToJsonCore(bytes, offset, builder);
						builder.Append("\"");
					}
					offset += num;
					num6 += num;
					builder.Append(":");
					num = MessagePackSerializer.ToJsonCore(bytes, offset, builder);
					offset += num;
					num6 += num;
					if ((long)num7 != (long)((ulong)(num5 - 1U)))
					{
						builder.Append(",");
					}
					num7++;
				}
				builder.Append("}");
				return num6;
			}
			case MessagePackType.Extension:
			{
				ExtensionResult extensionResult = MessagePackBinary.ReadExtensionFormat(bytes, offset, out num);
				if ((int)extensionResult.TypeCode == -1)
				{
					DateTime dateTime = MessagePackBinary.ReadDateTime(bytes, offset, out num);
					builder.Append("\"");
					builder.Append(dateTime.ToString("o", CultureInfo.InvariantCulture));
					builder.Append("\"");
				}
				else
				{
					builder.Append("[");
					builder.Append(extensionResult.TypeCode);
					builder.Append(",");
					builder.Append("\"");
					builder.Append(Convert.ToBase64String(extensionResult.Data));
					builder.Append("\"");
					builder.Append("]");
				}
				return num;
			}
			}
			num = 1;
			builder.Append("null");
			return num;
		}

		// Token: 0x06000391 RID: 913 RVA: 0x0000D2F0 File Offset: 0x0000B6F0
		private static void WriteJsonString(string value, StringBuilder builder)
		{
			builder.Append('"');
			int length = value.Length;
			for (int i = 0; i < length; i++)
			{
				char c = value[i];
				switch (c)
				{
				case '\b':
					builder.Append("\\b");
					break;
				case '\t':
					builder.Append("\\t");
					break;
				case '\n':
					builder.Append("\\n");
					break;
				default:
					if (c != '"')
					{
						if (c != '\\')
						{
							builder.Append(c);
						}
						else
						{
							builder.Append("\\\\");
						}
					}
					else
					{
						builder.Append("\\\"");
					}
					break;
				case '\f':
					builder.Append("\\f");
					break;
				case '\r':
					builder.Append("\\r");
					break;
				}
			}
			builder.Append('"');
		}

		// Token: 0x1700000E RID: 14
		// (get) Token: 0x06000392 RID: 914 RVA: 0x0000D3E9 File Offset: 0x0000B7E9
		public static IFormatterResolver DefaultResolver
		{
			get
			{
				if (MessagePackSerializer.defaultResolver == null)
				{
					MessagePackSerializer.defaultResolver = StandardResolver.Instance;
				}
				return MessagePackSerializer.defaultResolver;
			}
		}

		// Token: 0x1700000F RID: 15
		// (get) Token: 0x06000393 RID: 915 RVA: 0x0000D404 File Offset: 0x0000B804
		public static bool IsInitialized
		{
			get
			{
				return MessagePackSerializer.defaultResolver != null;
			}
		}

		// Token: 0x06000394 RID: 916 RVA: 0x0000D411 File Offset: 0x0000B811
		public static void SetDefaultResolver(IFormatterResolver resolver)
		{
			MessagePackSerializer.defaultResolver = resolver;
		}

		// Token: 0x06000395 RID: 917 RVA: 0x0000D419 File Offset: 0x0000B819
		public static byte[] Serialize<T>(T obj)
		{
			return MessagePackSerializer.Serialize<T>(obj, MessagePackSerializer.defaultResolver);
		}

		// Token: 0x06000396 RID: 918 RVA: 0x0000D428 File Offset: 0x0000B828
		public static byte[] Serialize<T>(T obj, IFormatterResolver resolver)
		{
			if (resolver == null)
			{
				resolver = MessagePackSerializer.DefaultResolver;
			}
			IMessagePackFormatter<T> formatterWithVerify = resolver.GetFormatterWithVerify<T>();
			byte[] buffer = InternalMemoryPool.GetBuffer();
			int newSize = formatterWithVerify.Serialize(ref buffer, 0, obj, resolver);
			return MessagePackBinary.FastCloneWithResize(buffer, newSize);
		}

		// Token: 0x06000397 RID: 919 RVA: 0x0000D462 File Offset: 0x0000B862
		public static ArraySegment<byte> SerializeUnsafe<T>(T obj)
		{
			return MessagePackSerializer.SerializeUnsafe<T>(obj, MessagePackSerializer.defaultResolver);
		}

		// Token: 0x06000398 RID: 920 RVA: 0x0000D470 File Offset: 0x0000B870
		public static ArraySegment<byte> SerializeUnsafe<T>(T obj, IFormatterResolver resolver)
		{
			if (resolver == null)
			{
				resolver = MessagePackSerializer.DefaultResolver;
			}
			IMessagePackFormatter<T> formatterWithVerify = resolver.GetFormatterWithVerify<T>();
			byte[] buffer = InternalMemoryPool.GetBuffer();
			int count = formatterWithVerify.Serialize(ref buffer, 0, obj, resolver);
			return new ArraySegment<byte>(buffer, 0, count);
		}

		// Token: 0x06000399 RID: 921 RVA: 0x0000D4AB File Offset: 0x0000B8AB
		public static void Serialize<T>(Stream stream, T obj)
		{
			MessagePackSerializer.Serialize<T>(stream, obj, MessagePackSerializer.defaultResolver);
		}

		// Token: 0x0600039A RID: 922 RVA: 0x0000D4BC File Offset: 0x0000B8BC
		public static void Serialize<T>(Stream stream, T obj, IFormatterResolver resolver)
		{
			if (resolver == null)
			{
				resolver = MessagePackSerializer.DefaultResolver;
			}
			IMessagePackFormatter<T> formatterWithVerify = resolver.GetFormatterWithVerify<T>();
			byte[] buffer = InternalMemoryPool.GetBuffer();
			int count = formatterWithVerify.Serialize(ref buffer, 0, obj, resolver);
			stream.Write(buffer, 0, count);
		}

		// Token: 0x0600039B RID: 923 RVA: 0x0000D4F8 File Offset: 0x0000B8F8
		public static T Deserialize<T>(byte[] bytes)
		{
			return MessagePackSerializer.Deserialize<T>(bytes, MessagePackSerializer.defaultResolver);
		}

		// Token: 0x0600039C RID: 924 RVA: 0x0000D508 File Offset: 0x0000B908
		public static T Deserialize<T>(byte[] bytes, IFormatterResolver resolver)
		{
			if (resolver == null)
			{
				resolver = MessagePackSerializer.DefaultResolver;
			}
			IMessagePackFormatter<T> formatterWithVerify = resolver.GetFormatterWithVerify<T>();
			int num;
			return formatterWithVerify.Deserialize(bytes, 0, resolver, out num);
		}

		// Token: 0x0600039D RID: 925 RVA: 0x0000D534 File Offset: 0x0000B934
		public static T Deserialize<T>(Stream stream)
		{
			return MessagePackSerializer.Deserialize<T>(stream, MessagePackSerializer.defaultResolver);
		}

		// Token: 0x0600039E RID: 926 RVA: 0x0000D544 File Offset: 0x0000B944
		public static T Deserialize<T>(Stream stream, IFormatterResolver resolver)
		{
			if (resolver == null)
			{
				resolver = MessagePackSerializer.DefaultResolver;
			}
			IMessagePackFormatter<T> formatterWithVerify = resolver.GetFormatterWithVerify<T>();
			byte[] buffer = InternalMemoryPool.GetBuffer();
			MessagePackSerializer.FillFromStream(stream, ref buffer);
			int num;
			return formatterWithVerify.Deserialize(buffer, 0, resolver, out num);
		}

		// Token: 0x0600039F RID: 927 RVA: 0x0000D580 File Offset: 0x0000B980
		private static int FillFromStream(Stream input, ref byte[] buffer)
		{
			int num = 0;
			int num2;
			while ((num2 = input.Read(buffer, num, buffer.Length - num)) > 0)
			{
				num += num2;
				if (num == buffer.Length)
				{
					MessagePackBinary.FastResize(ref buffer, num * 2);
				}
			}
			return num;
		}

		// Token: 0x04000151 RID: 337
		private static IFormatterResolver defaultResolver;
	}
}
